﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer.Core;
using TemplateFoundation.Core;
using TemplateFoundation.Tags;

namespace TemplateFoundation.WebControls
{
    /// <summary>
    /// Used to render a list of all tags on the website, or for a specific page
    /// </summary>
    public class TagList : WebControl
    {
        /// <summary>
        /// Creates a new TagList control
        /// </summary>
        public TagList() : base(HtmlTextWriterTag.Ul)
        {
            CssClass = "tags";
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            foreach (var tag in Tags)
            {
                if(PageReference.IsNullOrEmpty(tag.PageLink))
                {
                    continue;
                }

                var li = new HtmlGenericControl("li");

                li.Controls.Add(new HtmlAnchor
                                    {
                                        HRef = tag.PageLink.GetPage().LinkURL,
                                        InnerText = tag.Name
                                    });

                li.Attributes.Add("class", "tag-" + tag.PageCount);

                if (DisplayPageCount)
                {
                    li.Controls.Add(new Literal() {Text = string.Format(" ({0})", tag.PageCount)});
                }

                Controls.Add(li);
            }

            if(Controls.Count==0) // No tags found, do not render empty UL element
            {
                Visible = false;
                return;
            }
        }

        /// <summary>
        /// Gets the tags to render
        /// </summary>
        protected virtual TagCollection Tags
        {
            get
            {
                return PageReference.IsNullOrEmpty(PageLink) 
                    ? TagFactory.Instance.GetTags() // All tags
                    : PageLink.GetPage().Tags; // Tags for specific page
            }
        }

        /// <summary>
        /// Gets or sets the CSS class attribute for the tag list
        /// </summary>
        /// <remarks>A class called 'tags' will always be added to the end of the CssClass property value</remarks>
        public override string CssClass
        {
            get
            {
                var tagsClassAdded = base.CssClass
                                     .Split(' ')
                                     .Any(c => c.Equals("tags", StringComparison.OrdinalIgnoreCase));

                if (tagsClassAdded)
                {
                    return base.CssClass;
                }

                return string.Format("{0} {1}", base.CssClass, "tags").Trim();
            }
            set
            {
                base.CssClass = value;
            }
        }

        /// <summary>
        /// Gets or sets a specific page for which tags should be listed
        /// </summary>
        /// <remarks>If this property is not set, a list of all available tags will be listed</remarks>
        public PageReference PageLink { get; set; }

        /// <summary>
        /// Gets or sets whether the number of tagged pages should be included for each tag
        /// </summary>
        public bool DisplayPageCount { get; set; }
    }
}
